<template>
  <div class="content">
    <site-nav />
    <head-item />
    <NewConseQuence v-if="isFlag && illegalConditionFlag " />
    <illegal-list v-else :show-type="isFlag ? 2 : 1 " />
    <advantage-item />
    <guide-item />
    <friendly-link />
    <footer-item />
  </div>
</template>

<script>
import SiteNav from '../../layout/Nav'
import AdvantageItem from '../../layout/Advantage'
import GuideItem from '../../layout/Guide'
import FriendlyLink from '../../layout/FriendlyLink'
import FooterItem from '../../layout/Footer'
import HeadItem from '../home/component/Head.vue'
import NewConseQuence from './component/NewConseQuence'
import IllegalList from './component/IllegalList'
import { checkCondition } from '@/api/search/newConseQuence'
import { Base64 } from 'js-base64'
import { MessageBox } from 'element-ui'
export default {
  components: {
    SiteNav,
    IllegalList,
    AdvantageItem,
    GuideItem,
    FriendlyLink,
    FooterItem,
    HeadItem,
    NewConseQuence
  },
  data() {
    return {
      // 判断参数是否合法 true为合法
      isFlag: true,
      // 判断参数是否失效 true为有效
      illegalConditionFlag: true
    }
  },
  beforeRouteEnter(to, from, next) {
    // 条件合法性 和参数校验
    try {
      const data = JSON.parse(Base64.decode(to.params.data))
      if (!data.address || Object.keys(data.address).length === 0) {
        throw new Error()
      }
      const mainCategoryId = data.mainCategory.id
      const mainCategoryName = data.mainCategory.categoryName
      const childCategoryId = data.childCategory.id
      const childCategoryName = data.childCategory.categoryName
      const childCategoryUnit = data.childCategory.categoryUnit
      const standardProa = data.childCategory.standardProa
      const standardProb = data.childCategory.standardProb
      const standardProc = data.childCategory.standardProc
      const standardProd = data.childCategory.standardProd
      const standardProe = data.childCategory.standardProe
      const proa = data.info.proa
      const prob = data.info.prob
      const proc = data.info.proc
      const prod = data.info.prod
      const proe = data.info.proe
      const proaId = data.proaId
      const probId = data.probId
      const procId = data.procId
      const prodId = data.prodId
      const proeId = data.proeId
      checkCondition({
        mainCategoryId, mainCategoryName, childCategoryId, childCategoryName, childCategoryUnit, standardProa, standardProb, standardProc, standardProd, standardProe, proa, prob, proc, prod, proe, proaId, probId, procId, prodId, proeId
      }).then(res => {
        if (res.code === 0) {
          next(vm => {
            vm.illegalConditionFlag = true
          })
        } else {
          next(vm => {
            vm.illegalConditionFlag = false
          })
        }
      }).catch((e) => {
        next(vm => {
          vm.illegalConditionFlag = false
        })
      })
    } catch (e) {
      next(vm => {
        // url被篡改
        vm.isFlag = false
        MessageBox.alert('<strong>URL地址已被篡改，系统自动为您跳转到首页</strong>', '提示框', {
          dangerouslyUseHTMLString: true,
          callback: action => {
            vm.$router.push('/')
          }
        })
      })
    }
  }
}
</script>

<style>
</style>
